package com.zpp.wedding_admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zpp.wedding_admin.entity.CameraWork;
import com.zpp.wedding_admin.service.ICameraWorkService;
import com.zpp.wedding_common.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 朱盼盼
 * @since 2022-12-29
 */
@Api(description = "帖子管理")
@CrossOrigin
@RestController
@RequestMapping("/weddingadmin/work")
public class CameraWorkController {

    @Autowired
    private ICameraWorkService workService;

    @ApiOperation(value = "发布帖子")
    @PostMapping("addWork")
    public R addWork(@RequestBody CameraWork cameraWork) {
        boolean b = workService.save(cameraWork);
        if ( b ) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "审批通过")
    @PutMapping("pass/{id}/{reason}")
    public R pass(@PathVariable String id,
                  @PathVariable String reason) {
        if ( reason == null || reason == "") reason = "通过";
        boolean b = workService.pass(id, reason);
        if ( b ) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "审批不通过")
    @PutMapping("noPass/{id}/{reason}")
    public R noPass(@PathVariable String id, @PathVariable String reason) {
        boolean b = workService.noPass(id, reason);
        if ( b ) return R.ok().data("reason", reason);
        return R.error();
    }

    @ApiOperation(value = "通过id获取帖子")
    @GetMapping("getById/{id}")
    public R getById(@PathVariable String id) {
        List<CameraWork> list = workService.getById(id);
        return R.ok().data("workList", list);
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("getWorkPageVo/{currentPage}/{size}")
    public R getBannerPageVo(@PathVariable Long currentPage,
                             @PathVariable Long size) {

        Page<CameraWork> page = new Page<CameraWork>(currentPage, size);
        workService.page(page, new LambdaQueryWrapper<CameraWork>().orderByDesc(CameraWork::getWorkId));
        List<CameraWork> workList = page.getRecords();
        workList = getList(workList);
        long total = page.getTotal();
        return R.ok().data("workList", workList).data("total", total);
    }

    @ApiOperation(value = "分页条件查询活动列表")
    @PostMapping("getWorkPageQuery/{currentPage}/{size}")
    public R getBannerPageQuery(@PathVariable Long currentPage,
                                @PathVariable Long size,
                                @RequestBody CameraWork cameraWork) {
        // RequestBody将json转换为实体类
        LambdaQueryWrapper<CameraWork> lqw = new LambdaQueryWrapper<CameraWork>();
        lqw.orderByDesc(CameraWork::getWorkId);
        String workName = cameraWork.getHeader();
        lqw.like(!StringUtils.isEmpty(workName), CameraWork::getHeader, workName);
        Page<CameraWork> page = new Page<CameraWork>(currentPage, size);
        workService.page(page, lqw);
        List<CameraWork> workList = page.getRecords();
        workList = getList(workList);
        long total = page.getTotal();
        return R.ok().data("workList", workList).data("total", total);
    }

    public List<CameraWork> getList(List<CameraWork> workList) {
        Map<Integer, CameraWork> map = new HashMap<>();
        List<CameraWork> list = workService.getAllList();
        for (CameraWork cameraWork : list) {
            map.put(cameraWork.getWorkId(), cameraWork);
        }
        for (int i = 0; i < workList.size(); i++) {
            CameraWork cameraWork = workList.get(i);
            cameraWork = map.get(cameraWork.getWorkId());
            workList.set(i, cameraWork);
        }
        return workList;
    }
}
